package com.jisheng.xb.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jisheng.xb.common.CallHttpUtil;
import com.jisheng.xb.common.DateUtil;
import com.jisheng.xb.common.StringUtil;
import com.jisheng.xb.dao.ExchangeMapper;
import com.jisheng.xb.dao.SharesMapper;
import com.jisheng.xb.entity.Exchange;
import com.jisheng.xb.entity.Shares;
import com.jisheng.xb.service.CatchDataService;


@Service("catchDataService")
public class CatchDataServiceImpl implements CatchDataService {
	Logger logger = LoggerFactory.getLogger(CatchDataService.class);
	
	@Autowired
	private SharesMapper sharesMapper ;
	
	@Autowired 
	private ExchangeMapper exchangeMapper;
	
	@Override
	public List<Shares> findByShareNos(String shareNos, String type) {
		type= type==null?"sh":type;
		if(StringUtil.isNumeric(shareNos)){
			shareNos = type+shareNos;
		}
		String result = CallHttpUtil.sendGet(CallHttpUtil.DATA_PRE_URL+shareNos, "GBK");
		//TODO 需要处理数据
		return null;
	}

	@Override
	public List<Shares> saveByShareNos(String shareNos, String type) {
		logger.info("saveByShareNos --- shareNos"+shareNos+"-----type---"+type);
		if(StringUtil.isNullOrEmpty(shareNos)){
			logger.error("参数传递错误！");
		}
		if(StringUtil.isNumeric(shareNos)){
			shareNos = type+shareNos;
		}
		String result = CallHttpUtil.sendGet(CallHttpUtil.DATA_PRE_URL+shareNos, "GBK");
		saveShares(result);
		return null;
	}
	
	/**
	 * 装载实体数据
	 * */
	private List<Shares> loadShares(String str){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Shares> list = new LinkedList<Shares>();
		Date date = new Date();
		String[] sharesArr = str.split(";");
		for(String sharesStr : sharesArr){
			String subStr = sharesStr.split("=")[1];
			String no = sharesStr.split("=")[0].replace("hq_str_","");
			String[] datasArr = subStr.split(",");
			Shares shares = new Shares();
			shares.setNo(no);
			shares.setName(datasArr[0]);
			shares.settBeginPrice(Double.valueOf(datasArr[1]));
			shares.setyLastPrice(Double.valueOf(datasArr[2]));
			shares.setCurrentPrice(Double.valueOf(datasArr[3]));
			shares.settHighPrice(Double.valueOf(datasArr[4]));
			shares.settLowPrice(Double.valueOf(datasArr[5]));
			shares.settPreBuyPrice(Double.valueOf(datasArr[6]));
			shares.settPreSellPrice(Double.valueOf(datasArr[7]));
			shares.setInsertTime(date);
			shares.setUpdateTime(date);
			shares.setDoneCount(Long.valueOf(datasArr[8]));
			shares.setDoneMoney(Double.valueOf(datasArr[9]));
			list.add(shares);
		}
		
		return list;
	}
	
	/**
	 * 保存获取的数据信息
	 * @throws ParseException 
	 * 
	 * */
	private void saveShares(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		
		String[] sharesArr = str.split(";");
		for(String sharesStr : sharesArr){
			String subStr = sharesStr.split("=")[1];
			String no = sharesStr.split("=")[0].replace("hq_str_","");
			String[] datasArr = subStr.split(",");
			//一个交易日直插入一条数据
			Shares shares = sharesMapper.selectBySharesNo(no);
			if(shares!=null && sdf.format(shares.getInsertTime()).equals(sdf.format(new Date()))){
				//更新当前对象
				shares.setCurrentPrice(Double.valueOf(datasArr[3]));
				shares.settHighPrice(Double.valueOf(datasArr[4]));
				shares.settLowPrice(Double.valueOf(datasArr[5]));
				shares.setDoneCount(Long.valueOf(datasArr[8]));
				shares.setDoneMoney(Double.valueOf(datasArr[9]));
				sharesMapper.updateByPrimaryKey(shares);
			}else{
				//插入一条
				shares = new Shares();
				shares.setNo(no);
				shares.setName(datasArr[0]);
				shares.settBeginPrice(Double.valueOf(datasArr[1]));
				shares.setyLastPrice(Double.valueOf(datasArr[2]));
				shares.setCurrentPrice(Double.valueOf(datasArr[3]));
				shares.settHighPrice(Double.valueOf(datasArr[4]));
				shares.settLowPrice(Double.valueOf(datasArr[5]));
				shares.settPreBuyPrice(Double.valueOf(datasArr[6]));
				shares.settPreSellPrice(Double.valueOf(datasArr[7]));
				shares.setInsertTime(date);
				shares.setUpdateTime(date);
				shares.setDoneCount(Long.valueOf(datasArr[8]));
				shares.setDoneMoney(Double.valueOf(datasArr[9]));
				sharesMapper.insert(shares);
			}
			Exchange ec = new Exchange();
			ec.setSharesNo(shares.getNo());
			ec.setBuyer1Count(Integer.valueOf(datasArr[10]));
			ec.setBuyer1Price(Double.valueOf(datasArr[11]));
			ec.setBuyer2Count(Integer.valueOf(datasArr[12]));
			ec.setBuyer2Price(Double.valueOf(datasArr[13]));
			ec.setBuyer3Count(Integer.valueOf(datasArr[14]));
			ec.setBuyer3Price(Double.valueOf(datasArr[15]));
			ec.setBuyer4Count(Integer.valueOf(datasArr[16]));
			ec.setBuyer4Price(Double.valueOf(datasArr[17]));
			ec.setBuyer5Count(Integer.valueOf(datasArr[18]));
			ec.setBuyer5Price(Double.valueOf(datasArr[19]));
			
			ec.setSeller1Count(Integer.valueOf(datasArr[20]));
			ec.setSeller1Price(Double.valueOf(datasArr[21]));
			ec.setSeller2Count(Integer.valueOf(datasArr[22]));
			ec.setSeller2Price(Double.valueOf(datasArr[23]));
			ec.setSeller3Count(Integer.valueOf(datasArr[24]));
			ec.setSeller3Price(Double.valueOf(datasArr[25]));
			ec.setSeller4Count(Integer.valueOf(datasArr[26]));
			ec.setSeller4Price(Double.valueOf(datasArr[27]));
			ec.setSeller5Count(Integer.valueOf(datasArr[28]));
			ec.setSeller5Price(Double.valueOf(datasArr[29]));
			
			try {
				ec.setCurrtentDate(sdf.parse(datasArr[30]));
			} catch (ParseException e) {
				logger.error("时间转换出错");
				e.printStackTrace();
			}
			ec.setTime(datasArr[31]);
			exchangeMapper.insert(ec);
		}
		
	}
	

}
